<?php
/**
 * MakeBill.php
 * Author: luoji (867404364@qq.com)
 * Date: 2020-12-21
 * Time: 15:28
 */

namespace App\Http\Controllers\Api\Business;


use App\Exceptions\RunException;
use App\Http\Controllers\Base;
use App\Tables;

class MakeBill extends Base
{
    public function getList()
    {
        $list = Tables\OrderDetail::where('outsource_id' , '=', 0)
            ->select('order_detail.*')
            ->leftJoin('order', 'order.id', '=', 'order_detail.oid')
            ->where('order.dumped', Tables\Order::No_Dumped)
            ->where('order_detail.make_status', '!=', Tables\Order::Make_Status_Ok)
            ->orderBy('order_detail.id', 'desc')
            ->get();
        $r = [];
        foreach($list as $v) {
            $cc = $pp = [];
            foreach($v->consumables()->where('bid', $v['bid'])->get() as $c) {
                $cc[] = $c['c_name'];
            }
            foreach($v->postPrints()->where('bid', $v['bid'])->get() as $p) {
                $pp[] = $p['p_name'];
            }
            $r[] = [
                'id' => $v['id'],
                'customerName' => $v->order()->value('customer_name') ?? '',
                'businessName' => $v['b_name'],
                'outsourceName' => $v['outsource_name'],
                'content' => $v['content'],
                'consumable' => $cc,
                'postPrint' => $pp,
                'completeDate' => date('Y-m-d', strtotime($v['complete_date'])),
                'status' => $v['make_status'],
                'orderNo' => $v['oid'],
                'unit' => $v['unit'],
                'width' => $v['width'],
                'height' => $v['height'],
                'number' => $v['number'],
                'printNumber' => $v['print']+1,
                'lastPrintTime' => date('Y-m-d H:i:s'),
                'checkImg' => Tables\OrderDetail::getRightImg($v['check_img'])
            ];
        }
        return $this->responseSuccess($r);
    }

    public function create()
    {
        $id = (array) $this->request->post('id', []);
        if(empty($id)) {
            return $this->responseFail('无信息');
        }
        $details = Tables\OrderDetail::whereIn('id', $id)->get();
        if($details->isEmpty()) {
            return $this->responseFail('未找到信息！');
        }
        \DB::beginTransaction();
        try{
            foreach($details as $detail) {
                $detail['make_status'] = Tables\Order::Make_Status_Ing;
                $detail['update_time'] = date('Y-m-d H:i:s');
                $detail['print'] = $detail['print'] + 1;
                $detail['last_print_time'] = time();
                if(!(bool) $detail->save()) {
                    throw new RunException('操作失败！(1)');
                }
                $o = $detail->order;
                $o['make_status'] =  Tables\Order::Make_Status_Ing;
                $o['update_time'] = date('Y-m-d H:i:s');
                if(!(bool) $o->save()) {
                    throw new RunException('操作失败！(2)');
                }
            }
            \DB::commit();
        } catch (RunException $e) {
            \DB::rollBack();
            return $this->responseFail($e->getMessage());
        } catch (\Exception $e) {
            \DB::rollBack();
            return $this->responseFail('数据库错误');
        }
        return $this->responseSuccess();
    }
}